package com.dormms.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dormms.bean.DormAdminInfo;
import com.dormms.bean.StaffInfo;
import com.dormms.bean.StuInfo;
import com.dormms.bean.User;
import com.dormms.dto.UserRoleDto;
import com.dormms.service.DormAdminService;
import com.dormms.service.LoginService;
import com.dormms.service.StaffInfoService;
import com.dormms.service.StuInfoService;
import com.dormms.service.UserSerivce;

@Controller
public class CommonController {
	@Autowired
	private DormAdminService dormAdminService;
	@Autowired
	private StaffInfoService staffInfoService;
	@Autowired
	private StuInfoService stuInfoService;
	@Autowired
	private LoginService loginService;
	@Autowired
	private UserSerivce userSerivce;
	
	@RequestMapping("admin/updateDormAdminInfo")
	@ResponseBody
	public Integer updateDormAdminInfoByDormAdmin(DormAdminInfo dormAdminInfo) {
		return dormAdminService.updateDormAdminInfo(dormAdminInfo);
	}
	
	@RequestMapping("admin/getDormAdminInfo")
	@ResponseBody
	public DormAdminInfo getDormAdminInfoByDormAdmin(HttpSession session) {
		UserRoleDto userRoleDto = (UserRoleDto) session.getAttribute("userRoleDto");
		return dormAdminService.getDormAdminInfo(userRoleDto.getUserId());
	}
	
	@RequestMapping("admin/updatePassword")
	@ResponseBody
	public int updatePasswordByDormAdmin(String oldPassword, String password, HttpSession session) {
		UserRoleDto userRoleDto = (UserRoleDto) session.getAttribute("userRoleDto");
		if(loginService.isLegalUser(userRoleDto.getUserId(), oldPassword)) {
			User user = new User();
			user.setUserid(userRoleDto.getUserId());
			user.setPassword(password);
			return userSerivce.updateUser(user);
		}
		return 0;
	}
	
	@RequestMapping("staff/updateStaffInfo")
	@ResponseBody
	public Integer updateStaffInfoByStaff(StaffInfo staffInfo) {
		return staffInfoService.updateStaffInfo(staffInfo);
	}
	
	@RequestMapping("staff/getStaffInfo")
	@ResponseBody
	public StaffInfo getStaffInfoByStaff(HttpSession session) {
		UserRoleDto userRoleDto = (UserRoleDto) session.getAttribute("userRoleDto");
		return staffInfoService.queryStaffInfoByStaffId(userRoleDto.getUserId());
	}
	
	@RequestMapping("staff/updatePassword")
	@ResponseBody
	public int updatePasswordByStaff(String oldPassword, String password, HttpSession session) {
		UserRoleDto userRoleDto = (UserRoleDto) session.getAttribute("userRoleDto");
		if(loginService.isLegalUser(userRoleDto.getUserId(), oldPassword)) {
			User user = new User();
			user.setUserid(userRoleDto.getUserId());
			user.setPassword(password);
			return userSerivce.updateUser(user);
		}
		return 0;
	}
	
	@RequestMapping("stu/updateStuInfo")
	@ResponseBody
	public Integer updateStuInfoByStu(StuInfo stuInfo) {
		return stuInfoService.updateStuInfo(stuInfo);
	}
	
	@RequestMapping("stu/getStuInfo")
	@ResponseBody
	public StuInfo getStuInfoByStu(HttpSession session) {
		UserRoleDto userRoleDto = (UserRoleDto) session.getAttribute("userRoleDto");
		return stuInfoService.queryStuInfo(userRoleDto.getUserId());
	}
	
	@RequestMapping("stu/updatePassword")
	@ResponseBody
	public int updatePasswordByStu(String oldPassword, String password, HttpSession session) {
		UserRoleDto userRoleDto = (UserRoleDto) session.getAttribute("userRoleDto");
		if(loginService.isLegalUser(userRoleDto.getUserId(), oldPassword)) {
			User user = new User();
			user.setUserid(userRoleDto.getUserId());
			user.setPassword(password);
			return userSerivce.updateUser(user);
		}
		return 0;
	}
	
	@RequestMapping("super/getSuperInfo")
	@ResponseBody
	public UserRoleDto getSuperInfoBySuper(HttpSession session) {
		UserRoleDto userRoleDto = (UserRoleDto) session.getAttribute("userRoleDto");
		return userRoleDto;
	}
	
	@RequestMapping("super/updatePassword")
	@ResponseBody
	public int updatePasswordBySuper(String oldPassword, String password, HttpSession session) {
		UserRoleDto userRoleDto = (UserRoleDto) session.getAttribute("userRoleDto");
		if(loginService.isLegalUser(userRoleDto.getUserId(), oldPassword)) {
			User user = new User();
			user.setUserid(userRoleDto.getUserId());
			user.setPassword(password);
			return userSerivce.updateUser(user);
		}
		return 0;
	}
	
	@RequestMapping("/{roleName}/logout.do")
	public void logout() {
		
	}
}
